package controller

import (
	"encoding/json"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"gowork/cinemaTicket/model"
	"strconv"
)

type CinemaController struct {
	Ctx  iris.Context
	OnlineMovie *model.OnlineMovie
}

func (cc *CinemaController)PostGeneratemovietickets() mvc.Result {

	cc.OnlineMovie = new(model.OnlineMovie)

	showDateListStr := cc.Ctx.FormValue("showDateList")
	var showDateList []string
	err := json.Unmarshal([]byte(showDateListStr), &showDateList)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	showTimeListStr := cc.Ctx.FormValue("showTimeList")
	var showTimeList []string
	err = json.Unmarshal([]byte(showTimeListStr), &showTimeList)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	onlineMovieListStr := cc.Ctx.FormValue("onlineMovieList")
	var onlineMovieList []*model.OnlineMovie
	err = json.Unmarshal([]byte(onlineMovieListStr), &onlineMovieList)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	projectionRoomListStr := cc.Ctx.FormValue("projectionRoomList")
	var projectionRoomList []*model.ProjectionRoom
	err = json.Unmarshal([]byte(projectionRoomListStr), &projectionRoomList)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	cinemaStr := cc.Ctx.FormValue("cinema")
	var cinema *model.Cinema
	err = json.Unmarshal([]byte(cinemaStr), &cinema)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := cc.OnlineMovie.GenerateMovieTickets(showDateList, showTimeList, onlineMovieList, projectionRoomList, cinema)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (cc *CinemaController)PostQueryfilmseat() mvc.Result {

	cc.OnlineMovie = new(model.OnlineMovie)

	filmIdStr := cc.Ctx.FormValue("filmId")
	filmId, err := strconv.ParseInt(filmIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	projectionRoomIdStr := cc.Ctx.FormValue("projectionRoomId")
	projectionRoomId, err := strconv.ParseInt(projectionRoomIdStr, 10, 64)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := cc.OnlineMovie.QueryFilmSeat(filmId, projectionRoomId)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}

func (cc *CinemaController)PostBuyfilm() mvc.Result {

	cc.OnlineMovie = new(model.OnlineMovie)

	filmStr := cc.Ctx.FormValue("film")
	var film *model.Film
	err := json.Unmarshal([]byte(filmStr), &film)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	viewingSeatStr := cc.Ctx.FormValue("viewingSeat")
	var viewingSeat *model.ViewingSeat
	err = json.Unmarshal([]byte(viewingSeatStr), &viewingSeat)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	res, err := cc.OnlineMovie.BuyFilm(film, viewingSeat)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": res["message"], "data": nil, "code":res["code"],},
	}
}

func (cc *CinemaController)PostQueryonlinemovie() mvc.Result {

	cc.OnlineMovie = new(model.OnlineMovie)

	date := cc.Ctx.FormValue("date")

	res, err := cc.OnlineMovie.QueryOnlineMovie(date)
	if err != nil{
		return mvc.Response{
			Object : map[string]interface{}{"message": err.Error(), "data": nil, "code":500,},
		}
	}

	return mvc.Response{
		Object : map[string]interface{}{"message": "成功", "data": res, "code":"200",},
	}
}














































